package day20130911;

import javax.swing.JPanel;

import java.awt.BorderLayout;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JList;

import OracleDB.Zip;
import OracleDB.ZipCode;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Types;
import java.util.Vector;


public class Search extends JPanel {
	JList list = new JList();
	private JTextField textField;
	private Connection conn;
	
	
	

	public Search() {
		

		String url="jdbc:oracle:thin:@127.0.0.1:1521";
		String user="hr";
		String password = "hr";
		

		try {
			// 1) 오라클 드라이버 로딩
			Class.forName("oracle.jdbc.driver.OracleDriver");

			// 2) 오라클 DB와 연결 (connection 생성)
			// Connection이 Interface 이므로 new로 할 수 없음
			conn = DriverManager.getConnection(url, user, password);

		} catch (Exception ex) {
			ex.printStackTrace();
		}


		
		
		
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		textField = new JTextField();
		
		
		
		panel_1.add(textField, BorderLayout.CENTER);
		textField.setColumns(10);
		
		JButton btnNewButton = new JButton("버튼이란다");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
		
		//	Vector<ZipCode> myZip = getZip(String dong);
			for(int i=0; i<myZip.size(); i++) {
				System.out.println(myZip.get(i));
			}
			
			textField.setText(" ");
			
				
			}
		});
		panel_1.add(btnNewButton, BorderLayout.EAST);
		
		JPanel panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JScrollPane scrollPane = new JScrollPane();
		panel_2.add(scrollPane, BorderLayout.CENTER);
		
		
		scrollPane.setViewportView(list);

	}
	public Vector<ZipCode> getZip(String dong) {
		Vector<ZipCode> v = new Vector<ZipCode>();
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			// 3) Statement 생성
			String sql = "SELECT * FROM zipcode WHERE dong like ?";
			pstmt = conn.prepareStatement(sql);

			// parameterIndex 는 ?의 순번. 1부터 시작
			//
			// 이렇게 쓸 수 있어서 PreparedStatement이 편한 거임.
			//
			// pstmt.setInt(1, 5000);
			pstmt.setString(1, "%" + dong + "%"); // 숫자는 setInt()로 넣어줘야 함. ㄷㄷ

			// 4) Statement 실행
			rs = pstmt.executeQuery();

			// 5) 실행결과 출력
			while (rs.next()) {
				ZipCode zc = new ZipCode();
				zc.setZipcode(rs.getString("zipcode"));
				zc.setSido(rs.getString("sido"));
				zc.setGugun(rs.getString("gugun"));
				zc.setDong(rs.getString("dong"));
				zc.setBunji(rs.getString("bunji"));
				v.add(zc);
			}

		
			
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				rs.close();
				pstmt.close();
				conn.close();

			} catch (Exception ex2) {
				ex2.printStackTrace();
			}
			return v;
		}
		
	}

}
